/* 
This do file calculates the state-level weighted average avoided damages given current installations in each zip.
NOTES: All directories and paths should be set in the main "SetUpSubsidyCalcs.do" file.

*/

clear all

import delimited `"${data_path}/Generation_and_damages_withCO2_2021-03-05_eGrid_SR.csv"'

rename v7 inter
rename zip zipcode

*drop Puerto Rico, Hawaii, and Alaska
drop if state=="HI" | state=="AK" | state=="PR"

gen zip_total_dam = num_systems*total_damages

sort state
bysort state: egen state_num_systems = sum(num_systems)
bysort state: egen state_total_dam = sum(zip_total_dam)

gen state_wtd_avg_dam = state_total_dam/state_num_systems

collapse (mean) state_wtd_avg_dam, by(state)

*outsheet state state_wtd_avg_dam using `"${data_intermediate}/state_wtd_avg_dam_2019-11-25.csv"', comma replace
export excel using `"${scripts_subsidies}/Solar_Incentives_Data_no_severin_data.xlsx"', sheet("Annual_Damages_4kWsystem") sheetmodify cell(A2) keepcellfmt
